<?php

/**
 * This is the model class for table "serviceprice".
 *
 * The followings are the available columns in table 'serviceprice':
 * @property integer $id
 * @property integer $provider_id
 * @property string $fromDate
 * @property integer $price
 *
 * The followings are the available model relations:
 * @property Provider $provider
 */
class ServicePrice extends ActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return ServicePrice the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'serviceprice';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        return array(
                array('provider_id, fromDate, price', 'required', 'message'=> 'Kötelező kitölteni'),
                array('provider_id, price', 'numerical', 'integerOnly'=>true),
                array('price', 'numerical', 'min'=>0),
               // array('fromDate', 'unique', 'message'=> 'A dátum nem egyezhet másik dátumokkal.'),
                array('fromDate', 'type', 'type'=>'date', 'message'=>'Nem egy dátum', 'dateFormat'=> 'yyyy-MM-dd'),
                array('fromDate, price', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
                'provider' => array(self::BELONGS_TO, 'Provider', 'provider_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
                'id' => 'ID',
                'provider_id' => 'Szolgáltató',
                'fromDate' => 'Ettől a dátumtól',
                'price' => 'Ár',
        );
    }

    public function byProvider($provider){

        $providerId = ($provider instanceof Provider) ? $provider->id : $provider;

        $this->getDbCriteria()->mergeWith(array(
                'condition' => 'provider_id = :providerId',
                'params'=>array(':providerId'=> $providerId)
        ));

        return $this;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        $criteria=new CDbCriteria;

        $criteria->compare('fromDate',$this->fromDate,true);
        $criteria->compare('price',$this->price);

        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
                'sort'=>array(
                        'defaultOrder'=>'fromDate DESC',
                )
        ));
    }
}